import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import models.ActionItem;
import models.Idea;
import models.Organization;
import models.Plan;
import models.Request;
import models.User;

import org.junit.Test;

import antlr.collections.List;

import play.mvc.Before;
import play.mvc.Http.Response;
import play.mvc.Scope;
import play.mvc.Scope.RenderArgs;
import play.test.Fixtures;
import play.test.FunctionalTest;
import play.test.UnitTest;

/**
 * 
 * @author Sherif Ayad
 * 
 */
public class acceptAndRejectUserRequestToActionItem extends UnitTest {

	/**
	 * Clear the database first
	 */
	@Before
	public void delete() {
		Fixtures.deleteAll();
	}

	/**
	 * Test The acceptRequestToJoinAnActionItem(long, long, long, long) & the
	 * rejectRequestToJoinAnActionItem(long, long, long, long) methods in
	 * Application.java controller
	 */
	@Test
	public void testAcceptAndReject() {
		User planCreator = User.find("byUserName", "fmeawad").first();
		Plan testPlan = new Plan("Ayad's Test Plan",
				"Just a plan to test with", 0.0, new Date(), new Date(),
				planCreator).save();
		Idea idea = Idea.find("byTitle", "recruit more cooks").first();
		testPlan.addIdea(idea);
		testPlan.save();
		ActionItem item = new ActionItem("Just an action item to test with",
				new Date(), new Date()).save();
		testPlan.addActionItem(item.id);
		testPlan.save();
		User sendingUser = User.find("byUserName", "m_selim").first();
		Date invitationDate = new Date();
		User recievingUser = item.plan.creator;
		Organization current = item.plan.ideas.get(0).topic.entity.organization;
		String description = sendingUser.userName
				+ " requested to join that action item";
		Request aRequest = new Request(sendingUser, current, recievingUser,
				invitationDate, description).save();
		item.requestsToJoin.add(aRequest);
		item.save();

		// If Accepted
		assertEquals(item.requestsToJoin.size(), 1);
		assertEquals(item.requestsToJoin.get(0).sendingUser.userName, "m_selim");
		assertEquals(item.requestsToJoin.get(0).recievingUser.userName,
				"fmeawad");
		item.requestsToJoin.remove(0);
		item.save();
		assertEquals(item.requestsToJoin.size(), 0);
		item.users.add(sendingUser);
		item.save();
		assertEquals(item.users.size(), 1);
		assertEquals(item.users.get(0).userName, "m_selim");
		item.requestsToJoin.add(aRequest);
		item.users.remove(0);
		item.save();
		// If rejected
		assertEquals(item.requestsToJoin.size(), 1);
		assertEquals(item.users.size(), 0);

	}
}
